package demo1218.pm1;

import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;

/*
TreeSet<E>:集合中的元素按照自然排序(Comparable).
构造方法:
public TreeSet<>();
public TreeSet<>(Comparator<? super E> com);
 */
public class Demo_03 {
    public static void main(String[] args) {
        Set<Person> set = new TreeSet<>(new Comparator<Person>() {
            @Override
            public int compare(Person o1, Person o2) {
                if (o1.getAge()>o2.getAge()){
                    return 1;
                }else if (o1.getAge()<o2.getAge()){
                    return -1;
                }else {
                    return 0;
                }
            }
        });
        set.add(new Person("rose", 19));
        set.add(new Person("jack", 9));
        set.add(new Person("tom", 8));
        set.add(new Person("tom", 8));
        System.out.println(set);
    }

    public static void demo01() {
        Set<Integer> set = new TreeSet<>();
        set.add(32);
        set.add(2);
        set.add(-2);
        set.add(321);
        System.out.println(set);
    }
}
